/**
 * Created by djh on 2016/11/8.
 * 交易管理公共js
 *
 */
define(function(require,exports){
   var common = require("common");
	/**
	 * 订单列表页
	 */
    exports.orderList = function(){
		var $wrap = $(".list_wrap");
		common.leftNav({dl: 1,dd:0});
		require.async("page",function(page){
			//tabajax加载

			page.pageAjax($wrap,".tab-menu ul a",function($this,html){

				$this.parents(".tab-menu").find(".active").removeClass("active");
				$this.addClass("active");
			});
		});

		/**
		 * 确认订单
		 */
		$wrap.on("click",".confirmation",function(){
			var id = $(this).attr('data-id');
			layer.confirm('是否确定订单信息无误',['确定','取消'],function(){
				layer.load();
				$.post('/member/order/userSecondConfirmation',{id:id},function(data){
					layer.closeAll('loading');
					if(data.code == 0 || data.code=='success'){
						layer.msg('操作成功');
						document.location.reload();
					}else{
						layer.msg(data.message);
					}
				},'json');
			});
		});
		/**
		 * 确认收貨
		 */
		$wrap.on("click",".confirmDelivery",function(){
			var id = $(this).attr('data-id');
			layer.confirm('是否确定收货',['确定','取消'],function(){
				layer.load();
				$.post('/member/order/confirmDelivery',{id:id},function(data){
					layer.closeAll('loading');
					if(data.code == 0 || data.code=='success'){
						layer.msg('操作成功');
						document.location.reload();
					}else{
						layer.msg(data.message);
					}
				},'json');
			});
		});

		/*取消*/
		$wrap.on("click",'[data-act="cancel"]',function(){
			var href= this.href;
			var id= $(this).data("id");
			layer.confirm("您确定取消该订单？",function(){
				common.ajaxHandle({
					url:href,
					data:{id:id}
				})
			});
			return false;
		});

	};
	/**
	 * 购物车结算
	 */
    exports.createOrder = function(){
    /*	var person_com = require("person_com");
		var validate = require("validate");
		require("jArea");*/

		common.leftNav({dl: 1,dd:0});
		require.async(["person_com","template","validate","jArea"],function(person_com,template){
			/*购物车结算页 提交订单*/
			$(".order_pay").on('click','.pay_btn',function () {
				$(".form").submit();
			});
			$(".form").validate({
				rules:{
					"shipping[shipping_fullname]":{required:true},
					"shipping[shipping_mobile]":{required:true,isMobile:true},
					"shipping[shipping_car_card]":{required:true,isCarNo:true},
					"shipping[shipping_id_card]":{required:true,cretID:[true]},
					"shipping[shipping_time]":{required:true}
				},submitHandler:function(){
					var size = $(".order_pay").find("[type='radio']:checked").size();
					var $form = $(".order_pay").find(".form");
					if(size<=0) {
						layer.alert("请选择收货地址！！");
					}else {
						common.ajaxHandle({
							url: $form.attr("action"),
							data:$form.serialize(),
							callback:function(data){
								layer.msg(data.message,{end:function(){
									location.href="/member/order/dasuPayOrder?type=1&id="+data.data;
								}});
							}
						})
					}
				}
			});

			//修改与新增收货地址
			$("[data-act='editAddr']").on("click",function(){
			var id = $(this).data("id"),title,data;
			if(!!id) {
				title = "编辑收货地址";
				common.ajaxHandle({
					url:"/member/addressInfo/editAjax",
					data:{id:id},
					async: false,
					callback:function(item){
						data=item.data;
						data["edit"]=true;
					}
				})
			}else {
				title = "新增收货地址";
				data={edit:false};
			}

			var temp = template("addrTemp",data);
			layer.open({
				title:title,
				btn: null,
				area:["720px","580px"],
				content:temp,
				success:function(layero,index){
					person_com.addrInit(data);
					layero.find("[data-act='cancel']").on("click",function(){
						layer.close(index);
					})

				}
			});
			return false;

		})
		});

	};

//点击收货人信息的样式
    $(".pay_addr dl").on("click",function () {console.log($(this).find("input[type='radio']").is(":checked"))
        /*var radio_c = $(".pay_addr dl").find("input[type='radio']").is(":checked");
        console.log(radio_c);*/
        $(this).addClass("pit_on");
        $(this).siblings().removeClass("pit_on");
        $(this).find("dd.amend").css({"display":"block"});
        $(this).siblings().find("dd.amend").css({"display":"none"});
    });

	/**
	 * 大塑支付
	 */
	exports.dasuPay = function() {
		common.leftNav({dl: 1, dd: 0});
		if(exports.wrong!="") {
			common.error(exports.wrong);
		}
		//提交锁定页面
		$(".pay_center").on("submit",function(){
			var val="";
			$('.fake-box input').each(function(i,e){
				val=val+e.value;
			});
			if(val.length<6) {
				common.error("请输入6位数字的交易密码！");
				return false;
			}
			$("#pwd").val(val);
			layer.load();
		});
		/**
		 * 密码框输入操作
         */
		$(".fake-box input").on("keydown",function(e){
			e = e || window.event; // FF、Chrome IE下获取事件对象
			var c = e.charCode || e.keyCode; //FF、Chrome IE下获取键盘码
			if ((c != 8 && c != 46 && // 8 - Backspace, 46 - Delete
				(c < 37 || c > 40) && // 37 (38) (39) (40) - Left (Up) (Right) (Down) Arrow
				(c < 48 || c > 57) && // 48~57 - 主键盘上的0~9
				(c < 96 || c > 105)))// 96~105 - 小键盘的0~9
			{
				e.preventDefault ? e.preventDefault() : e.returnValue = false; // 阻止事件传播到keypress
			}else if(c == 8 || c == 46) {// 8 - Backspace, 46 - Delete
				if($(this).val()=="") {
					e.preventDefault ? e.preventDefault() : e.returnValue = false; // 阻止事件传播到keypress
					$(this).prev().focus().val("");
				}
			}
		});
		$(".fake-box input").on("keyup",function(e){
			e = e || window.event; // FF、Chrome IE下获取事件对象
			var c = e.charCode || e.keyCode; //FF、Chrome IE下获取键盘码
			if (
				(c < 37 || c > 40) && // 37 (38) (39) (40) - Left (Up) (Right) (Down) Arrow
				(c < 48 || c > 57) && // 48~57 - 主键盘上的0~9
				(c < 96 || c > 105))// 96~105 - 小键盘的0~9
			{
				e.preventDefault ? e.preventDefault() : e.returnValue = false; // 阻止事件传播到keypress
			}else if($(this).hasClass("last")) {
				$(".pay_btn").focus();
			}else if($(this).val()!=""){
				$(this).next().focus();
			}

		});
	};
});



















